home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual Basic 5 / Mastering Microsoft Visual Basic 5.ISO / labs / lab04 / categories.frm (.txt) next >
Encoding:
Visual Basic Form  |  1997-01-24  |  7.5 KB  |  255 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Categories"
  4.    ClientHeight    =   3435
  5.    ClientLeft      =   3450
  6.    ClientTop       =   2325
  7.    ClientWidth     =   5010
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   3435
  10.    ScaleWidth      =   5010
  11.    Begin VB.CommandButton cmdClose 
  12.       Caption         =   "C&lose"
  13.       Height          =   375
  14.       Left            =   3600
  15.       TabIndex        =   10
  16.       Top             =   3000
  17.       Width           =   1335
  18.    End
  19.    Begin VB.CommandButton cmdFind 
  20.       Caption         =   "&Find"
  21.       Height          =   375
  22.       Left            =   3600
  23.       TabIndex        =   9
  24.       Top             =   2520
  25.       Width           =   1335
  26.    End
  27.    Begin VB.CommandButton cmdDelete 
  28.       Caption         =   "&Delete"
  29.       Height          =   375
  30.       Left            =   3600
  31.       TabIndex        =   8
  32.       Top             =   2040
  33.       Width           =   1335
  34.    End
  35.    Begin VB.CommandButton cmdCancel 
  36.       Caption         =   "&Cancel"
  37.       Height          =   375
  38.       Left            =   3600
  39.       TabIndex        =   7
  40.       Top             =   1560
  41.       Width           =   1335
  42.    End
  43.    Begin VB.CommandButton cmdEdit 
  44.       Caption         =   "&Edit"
  45.       Height          =   375
  46.       Left            =   3600
  47.       TabIndex        =   5
  48.       Top             =   600
  49.       Width           =   1335
  50.    End
  51.    Begin VB.CommandButton cmdSave 
  52.       Caption         =   "&Save"
  53.       Height          =   375
  54.       Left            =   3600
  55.       TabIndex        =   6
  56.       Top             =   1080
  57.       Width           =   1335
  58.    End
  59.    Begin VB.CommandButton cmdAdd 
  60.       Caption         =   "&Add"
  61.       Height          =   375
  62.       Left            =   3600
  63.       TabIndex        =   4
  64.       Top             =   120
  65.       Width           =   1335
  66.    End
  67.    Begin VB.CommandButton cmdNext 
  68.       Caption         =   "Move Next >"
  69.       Height          =   375
  70.       Left            =   1920
  71.       TabIndex        =   3
  72.       Top             =   1680
  73.       Width           =   1455
  74.    End
  75.    Begin VB.CommandButton cmdPrevious 
  76.       Caption         =   "< Move Previous"
  77.       Height          =   375
  78.       Left            =   360
  79.       TabIndex        =   2
  80.       Top             =   1680
  81.       Width           =   1455
  82.    End
  83.    Begin VB.TextBox txtCategoryName 
  84.       Height          =   285
  85.       Left            =   1440
  86.       TabIndex        =   0
  87.       Top             =   600
  88.       Width           =   2055
  89.    End
  90.    Begin VB.TextBox txtDescription 
  91.       Height          =   285
  92.       Left            =   1440
  93.       TabIndex        =   1
  94.       Top             =   1080
  95.       Width           =   2055
  96.    End
  97.    Begin VB.Label Label4 
  98.       Caption         =   "&Description:"
  99.       Height          =   255
  100.       Left            =   120
  101.       TabIndex        =   14
  102.       Top             =   1200
  103.       Width           =   975
  104.    End
  105.    Begin VB.Label Label3 
  106.       Caption         =   "Category &Name:"
  107.       Height          =   255
  108.       Left            =   120
  109.       TabIndex        =   13
  110.       Top             =   720
  111.       Width           =   1215
  112.    End
  113.    Begin VB.Label Label2 
  114.       Caption         =   "Category &ID"
  115.       Height          =   255
  116.       Left            =   120
  117.       TabIndex        =   12
  118.       Top             =   240
  119.       Width           =   975
  120.    End
  121.    Begin VB.Label lblCategoryID 
  122.       BorderStyle     =   1  'Fixed Single
  123.       Height          =   285
  124.       Left            =   1440
  125.       TabIndex        =   11
  126.       Top             =   120
  127.       Width           =   2055
  128.    End
  129. Attribute VB_Name = "Form1"
  130. Attribute VB_GlobalNameSpace = False
  131. Attribute VB_Creatable = False
  132. Attribute VB_PredeclaredId = True
  133. Attribute VB_Exposed = False
  134. Dim dbCurrent As Database
  135. Dim recCategories As Recordset
  136. Private Sub cmdAdd_Click()
  137.     ' add a new record
  138.     recCategories.AddNew
  139.     lblCategoryID.Caption = recCategories.Fields("CategoryID")
  140.     txtCategoryName.Text = ""
  141.     txtDescription.Text = ""
  142.     ButtonEditAddMode
  143.     txtCategoryName.SetFocus
  144. End Sub
  145. Private Sub cmdCancel_Click()
  146.     recCategories.CancelUpdate
  147.     FillFields
  148.     ButtonNonEditAddMode
  149. End Sub
  150. Private Sub cmdClose_Click()
  151.     Unload Me
  152. End Sub
  153. Private Sub cmdDelete_Click()
  154.     recCategories.Delete
  155.     recCategories.MoveNext
  156.     If recCategories.EOF Then
  157.         recCategories.MoveLast
  158.     End If
  159.     FillFields
  160. End Sub
  161. Private Sub cmdEdit_Click()
  162.     recCategories.Edit
  163.     ButtonEditAddMode
  164. End Sub
  165. Private Sub cmdFind_Click()
  166.     Dim strSQL As String
  167.     Dim strAnswer As String
  168.     strAnswer = InputBox("Enter any portion of the Description", "Find Records")
  169.     strSQL = "Select * from categories where [Description] like " & _
  170.         "'*" & strAnswer & "*'"
  171.     ' run the query
  172.     Set recCategories = dbCurrent.OpenRecordset(strSQL)
  173.     If recCategories.RecordCount = 0 Then
  174.         'no records found
  175.         MsgBox "No matching records found. Displaying all records."
  176.         Set recCategories = dbCurrent.OpenRecordset("Categories")
  177.     Else
  178.         'at least one record was found
  179.         recCategories.MoveFirst
  180.         FillFields
  181.     End If
  182. End Sub
  183. Private Sub cmdNext_Click()
  184.     recCategories.MoveNext
  185.     If recCategories.EOF Then
  186.         Beep
  187.         recCategories.MoveLast
  188.     Else
  189.         FillFields
  190.     End If
  191. End Sub
  192. Private Sub cmdPrevious_Click()
  193.     recCategories.MovePrevious
  194.     If recCategories.BOF Then
  195.         Beep
  196.         recCategories.MoveFirst
  197.     Else
  198.         FillFields
  199.     End If
  200. End Sub
  201. Private Sub cmdSave_Click()
  202.     ' save the record
  203.     recCategories.Fields("CategoryName") = txtCategoryName.Text
  204.     recCategories.Fields("Description") = txtDescription.Text
  205.     recCategories.Update
  206.     recCategories.Bookmark = recCategories.LastModified
  207.     ButtonNonEditAddMode
  208. End Sub
  209. Private Sub Form_Load()
  210.     ' open the Northwind Database
  211.     Set dbCurrent = OpenDatabase("..\..\Nwind.mdb")
  212.     ' create a recordset based on the Categories table
  213.     Set recCategories = dbCurrent.OpenRecordset("Categories")
  214.     ' move to the firecCategoriest record in the recordset
  215.     recCategories.MoveFirst
  216.     FillFields
  217.     ButtonNonEditAddMode
  218. End Sub
  219. Sub FillFields()
  220.     ' populate the form with data from the recordset
  221.     lblCategoryID.Caption = recCategories.Fields("CategoryID")
  222.     txtCategoryName.Text = recCategories.Fields("CategoryName")
  223.     txtDescription.Text = recCategories.Fields("Description")
  224. End Sub
  225. Private Sub Form_Unload(Cancel As Integer)
  226.     ' close the database
  227.     dbCurrent.Close
  228. End Sub
  229. Sub ButtonEditAddMode()
  230.     cmdSave.Enabled = True
  231.     cmdCancel.Enabled = True
  232.     cmdAdd.Enabled = False
  233.     cmdEdit.Enabled = False
  234.     cmdDelete.Enabled = False
  235.     cmdFind.Enabled = False
  236.     cmdClose.Enabled = False
  237.     cmdPrevious.Enabled = False
  238.     cmdNext.Enabled = False
  239.     txtCategoryName.Enabled = True
  240.     txtDescription.Enabled = True
  241. End Sub
  242. Sub ButtonNonEditAddMode()
  243.     cmdSave.Enabled = False
  244.     cmdCancel.Enabled = False
  245.     cmdAdd.Enabled = True
  246.     cmdEdit.Enabled = True
  247.     cmdDelete.Enabled = True
  248.     cmdFind.Enabled = True
  249.     cmdClose.Enabled = True
  250.     cmdPrevious.Enabled = True
  251.     cmdNext.Enabled = True
  252.     txtCategoryName.Enabled = False
  253.     txtDescription.Enabled = False
  254. End Sub
  255.